<%@ page import="pojo.Score" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %><%--
  Created by IntelliJ IDEA.
  User: 21131
  Date: 2023/11/26
  Time: 22:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>查看这门考试的所有学生的成绩</title>
    <style>
        table {
            width: 1000px;
            text-align: center;
            margin-top: 20px;
            margin-left: 20%;
        }

        /*每个单元格*/
        th, td {
            border: 1px solid #999;
            text-align: center;
            padding: 20px 0;
            height: 40px;
        }

        /*偶数的tr的颜色*/
        table tr:nth-child(odd) {
            background-color: rgba(12, 163, 255, 0.49);
        }

        /*鼠标滑过的颜色*/
        table tr:hover {
            background-color: rgba(12, 163, 255, 0.39);
        }

        /*表格内的按钮*/
        button {
            height: 38px;
            width: 90px;
            background-color: white;
        }

        #mytable th {
            text-align: center;
            width: 200px;
        }

        /*搜索框*/
        .content_input .my-search {
            margin-left: 10px;
            margin-top: 20px;
            float: left;
            width: 30%;
            height: 38px;
            border-radius: 5px;
            color: dimgray;
        }

        .content_input .search_btn { /*按钮样式*/
            width: 80px;
            height: 40px;
            margin-top: 20px;
            margin-left: 30px;
            background-color: #428bca;
        }

        .content_input .search_btn:hover { /*按钮覆盖样式*/
            background-color: #2e6da4;
            color: ivory;
        }

        /*查看按钮*/
        .chakan {
            background-color: rgba(12, 163, 255, 0.49);
            border: 2px solid black;
            height: 38px;
            width: 90px;
        }

        /*查看按钮悬浮颜色*/
        .chakan:hover {
            background-color: rgba(11, 114, 193, 0.25);
        }

        /*修改按钮*/
        .change {
            background-color: rgba(12, 163, 255, 0.59);
            border: 2px solid black;
            height: 38px;
            width: 90px;
        }

        /*修改按钮悬浮颜色*/
        .change:hover {
            background-color: rgba(11, 114, 193, 0.25);
        }

        /* 遮罩层 */
        #overlay {
            position: fixed;
            left: 20px;
            top: 40px;
            width: 100%;
            height: 100%;
            font-size: 16px;
            background-color: rgba(0, 0, 0, 0.2);
            /*display: none;*/
        }

        /* 弹出框主体 */
        .popup {
            background-color: #ffffff;
            max-width: 400px;
            min-width: 200px;
            height: 350px;
            border-radius: 5px;
            margin: 100px auto;
            text-align: center;
        }

        /* 弹出框的标题 */
        .popup_title {
            height: 60px;
            line-height: 60px;
            border-bottom: solid 1px #cccccc;
            font-size: 25px;
        }

        /* 弹出框的内容 */
        .popup_content {
            height: 50px;
            line-height: 50px;
            padding: 15px 20px;
        }

        /* 弹出框的按钮栏 */
        .popup_btn {
            padding-bottom: 10px;
        }

        /* 弹出框的按钮 */
        .popup_btn button {
            color: #778899;
            width: 40%;
            height: 40px;
            cursor: pointer;
            border: solid 1px #cccccc;
            border-radius: 5px;
            margin: 40px 10px;
            color: #ffffff;
            background-color: #337ab7;
        }
    </style>
</head>
<body>
<jsp:include page="teacher_menu.jsp"></jsp:include>
<div style="background-color: #68afe1;display: inline-block;margin-top: 10px;float: left;margin-left: 20%;height: 80px;width: 200px;text-align: center; border-radius:0px;">
    <h1 style="color:white;text-shadow: 2px 2px 2px dodgerblue;line-height: 40px;border-radius:2px;font-size: 25px">
        学生成绩</h1>
</div>
<div style="margin-left: 35%;margin-top:10px;width: 60%;height:80px;background-color: #68afe1 ">
    <form style="padding-left: 10%" action="${pageContext.request.contextPath }/search_StudentScoreBys_name"
          method="POST">
        <div class="content_input">
            <input class="my-search" type="text" placeholder="请输入学生姓名" name="s_name">
            <input type="submit" class="btn btn-primary search_btn" value="查询">
            <form style="padding-left: 10%" action="${pageContext.request.contextPath }/loadMyStudentsScoreList"
                  method="POST">
                <input type="submit" value="所有" class="btn btn-primary search_btn">
            </form>
        </div>
    </form>
</div>
<a href="teacher_score.jsp"
   style="display: inline-block;margin-left:20%;margin-top:2px;margin-bottom:2px;height: 30px;text-decoration: none;line-height: 30px; background-color: #66afe9; font-size: 15px; float: left;color: black"><strong><</strong>返回上一级</a>
<table id="mytable">
    <tr style="background-color:#66afe9;color: white">
        <th>试卷名称</th>
        <th>学生学号</th>
        <th>学生姓名</th>
        <th>成绩</th>
        <th>查看试卷</th>
        <th>修改成绩</th>
    </tr>
    <%
        List<Score> cs = (List<Score>) session.getAttribute("all_studentsScore");
        String p_name = session.getAttribute("my_p_name").toString();
        if (cs.isEmpty()) {//数据为空
    %>
    <tr>
        <td>此</td>
        <td>处</td>
        <td>数</td>
        <td>据</td>
        <td>为</td>
        <td>空</td>
    </tr>
    <%
        }
        for (Score c : cs) {
            String name = session.getAttribute("name" + c.getS_id()).toString();
    %>
    <tr>
        <td><%=p_name%>
        </td>
        <td><%=c.getS_id()%>
        </td>
        <td><%=name%>
        </td>
        <td><%=c.getScore()%>
        </td>
        <td>
            <input type="button" id="chakan" class="chakan" value="查看">
        </td>
        <td>
            <input type="button" id="change" class="change" value="修改">
        </td>
        <%
            }
        %>
    </tr>
</table>
<%--//修改弹出的窗口 --%>
<div id="overlay" style="display: none">
    <div class="popup">
        <p class="popup_title">修改成绩</p>
        <p class="popup_content">试卷名称：<input id="span1" type="text" style="height: 40px" value="" disabled></p>
        <p class="popup_content">学生姓名：<input id="span2" type="text" style="height: 40px" value="" disabled></p>
        <p class="popup_content">学生成绩：<input id="span3" type="text"  style="height: 40px" value=""
                                                 oninput="validateInput(this)"></p>
        <div class="popup_btn">
            <button class="cancelBtn" onclick="hidePopup()">取消</button>
            <button class="confirmBtn" id="confirm1">确认</button>
        </div>
    </div>
</div>
</body>
</html>
<script src="js/jquery-1.12.3.min.js"></script>
<script>
    function hidePopup() {
        var overlay = document.getElementById("overlay");
        overlay.style.display = "none";
    }
    function validateInput(input) {//限制修改成绩的成绩输入框只能输入数字和一个小数点并且不能输入负数
        input.value = input.value.replace(/[^\d.]/g, ''); // 只保留数字和小数点
        input.value = input.value.replace(/^\./, ''); // 去除开头的小数点
        input.value = input.value.replace(/\.{2,}/g, '.'); // 去除多余的小数点
        input.value = input.value.replace(/^0+(\d)/, '$1'); // 去除开头多余的零
        input.value = input.value.replace(/^0+\.$/, '0.'); // 处理只输入一个点的情况
        input.value = input.value.replace(/^0+(\d+\.\d*|\.(\d*))$/, '$1$2'); // 处理只输入零的情况

        // 验证输入是否符合条件
        if (isNaN(input.value) || parseFloat(input.value) <= 0) {
            input.setCustomValidity('请输入大于0的数字');
        } else {
            input.setCustomValidity('');
        }
    }
    // 获取点击查看的试卷名和学生id
    $(function () {
        let name1;
        let s_id;
        let s_name;
        $(".chakan").click(function () {
            name1 = $(this).parents("tr").children("td:nth-child(1)").text();//获取对应的试卷名
            s_id = $(this).parents("tr").children("td:nth-child(2)").text();//获取对应的学号
            s_name = $(this).parents("tr").children("td:nth-child(3)").text();//获取对应的姓名
            $.ajax({//传数据给controller判断是否已发布
                type: "post",
                url: "${pageContext.request.contextPath }/teacherToCorrectedExam",
                data: {
                    "name": name1,
                    "s_id": s_id,
                    "s_name": s_name
                },
                async: true,
                success: function (data) {
                    console.log(data);//在控制台输出信息。
                    if (data == true) {//已发布
                        window.location.replace("${pageContext.request.contextPath }/corrected_exam.jsp");
                        // alert("查看成功")
                    } else {//还未发布
                        alert("查看失败")
                    }
                }
            });

        })
        //修改学生成绩的信息
        $(".change").click(function () {
            name1 = $(this).parents("tr").children("td:nth-child(1)").text();//获取对应的试卷名
            s_id = $(this).parents("tr").children("td:nth-child(2)").text();//获取对应的学号
            s_name = $(this).parents("tr").children("td:nth-child(3)").text();//获取对应的姓名
            overlay = document.getElementById("overlay");//显示弹窗
            overlay.style.display = "block";
            $("#span1").val(name1);//显示要修改的试卷名
            $("#span2").val(s_name);//显示要修改的学生姓名
            $.ajax({//传数据给controller判断是否已发布
                type: "post",
                url: "${pageContext.request.contextPath }/changeInfo",
                data: {
                    "name": name1,
                    "s_id":s_id,
                    "s_name":s_name
                },
                async: true,
                success: function (data) {
                    console.log(data);//在控制台输出信息。
                    if (data == true) {//传输成功
                    } else {//传输成功
                        alert("传输失败")
                    }
                }
            });
        })
        //修改学生成绩
        $(".confirmBtn").click(function () {
            var score1= $("#span3").val();//成绩不能为空
            if(score1==""){
                alert('成绩不能为空')
            }
            else{
                $.ajax({//传数据给controller修改
                    type: "post",
                    url: "${pageContext.request.contextPath }/changeScore",
                    data: {
                        "score1":score1
                    },
                    async: true,
                    success: function (data) {
                        console.log(data);//在控制台输出信息。
                        if (data == true) {
                            alert("修改成功")
                            window.location.replace("${pageContext.request.contextPath }/loadMyStudentsScoreList");//及时刷新页面
                        } else {
                            alert("修改失败")
                            window.location.replace("${pageContext.request.contextPath }/loadMyStudentsScoreList");//及时刷新页面
                        }
                    }
                });
                var overlay = document.getElementById("overlay");
                overlay.style.display = "none";
            }

        })
    })
</script>